import java.awt.*;
import java.util.LinkedList;
import java.util.Scanner;

//孩子们的游戏
public class test3 {
    static class lastKid{
        public int number;
        public lastKid next;//指向下一个孩子

        public lastKid(int number){
            this.number = number;
            this.next=null;
        }

    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();//孩子数量
        int m = scanner.nextInt();//标识数
        int lastKid = LastRemaining_Solution(n,m);
        System.out.println(lastKid);
    }

    public static int LastRemaining_Solution (int n, int m) {
        int last;
        //头节点
        lastKid lastKid = null;
        lastKid cur = null;
        for (int i = 0; i <n ; i++) {
            lastKid kid = new lastKid(i);
            if (lastKid==null){
                lastKid=kid;
                cur=kid;
            }else{
                cur.next=kid;
                cur=kid;
            }
        }
        cur.next=lastKid;

        lastKid cur1 = lastKid;
        while (true){
            int count = 1;
            while (count<m){
                cur1=cur1.next;
                count++;
            }
            lastKid remove = cur1;//要删除的节点
            cur1 = cur1.next;
            remove.next=null;
            if(cur1==lastKid){
                last = cur1.number;
                break;
            }
        }
        return last;
    }
}
